package com.example.novelsys.controller;

import com.example.novelsys.entity.Favorites;
import com.example.novelsys.entity.Novel;
import com.example.novelsys.entity.Readlog;
import com.example.novelsys.entity.Users;
import com.example.novelsys.service.FavoriteService;
import com.example.novelsys.service.NovelService;
import com.example.novelsys.service.ReadLogService;
import com.example.novelsys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MemberController {
    @Autowired
    FavoriteService favoriteService;
    @Autowired
    NovelService novelService;
    @Autowired
    ReadLogService readLogService;
    @Autowired
    UserService userService;


    @RequestMapping("/toMemberInfo")
    public String memberPage(@RequestParam(value = "pageNav",required = false,defaultValue ="0") Integer pageNav, HttpServletRequest req) {

        //1、获取session中的用户信息
        HttpSession session = req.getSession();
        Users user = (Users) session.getAttribute("user");
        Users usernowInfo = userService.queryByKey(user.getUserId());

        req.setAttribute("usernowInfo", usernowInfo);

        //2、该用户收藏信息
        List<Favorites> favoriteList = favoriteService.queryByID(user.getUserId());

        //3、该用户发表的小说
        List<Novel> novelList = novelService.queryByID(user.getUserId());

        //4、该用户阅读记录
        List<Readlog> readLogList = readLogService.queryByID(user.getUserId());
        System.out.println(readLogList);

        List memberInfoList = new ArrayList();
        memberInfoList.add(favoriteList);
        memberInfoList.add(novelList);
        memberInfoList.add(readLogList);

        req.setAttribute("memberInfoList", memberInfoList);



        if (pageNav == 1) {
            return "userInfo";
        } else if (pageNav == 2) {
            return "eBookInfo";
        } else if (pageNav == 3) {
           return "myfavorite";
        } else if (pageNav == 4) {
            return "readhistory";
        } else {
            String flag = req.getParameter("flag");
            if (flag != null) {
                req.setAttribute("flag", flag);
            }

            return "memberInfo";
        }


    }
}
